package com.util;

import java.awt.image.*;


public class ImagesPlayer
{
  private String imName;
  private boolean isRepeating, ticksIgnored;
  private ImagesLoader imsLoader;
  private int animPeriod;
  private long animTotalTime;
  private int showPeriod;
  private double seqDuration;
  private int numImages;
  private int imPosition;

  private ImagesPlayerWatcher watcher = null;

  public ImagesPlayer(String nm, int ap, double d, boolean isr, ImagesLoader il)
  {
    imName = nm;
    animPeriod = ap;
    seqDuration = d;
    isRepeating = isr;
    imsLoader = il;

    animTotalTime = 0L;

    if (seqDuration < 0.5) {
      System.err.println("Warning: minimum sequence duration is 0.5 sec.");
      seqDuration = 0.5;
    }

    if (!imsLoader.isLoaded(imName)) {
      System.err.println(imName + " is not known by the ImagesLoader");
      numImages = 0;
      imPosition = -1;
      ticksIgnored = true;
    }
    else {
      numImages = imsLoader.numImages(imName);
      imPosition = 0;
      ticksIgnored = false;
      showPeriod = (int) (1000 * seqDuration / numImages);
    }
  }

  public void updateTick() {
    if (!ticksIgnored) {

      animTotalTime = (animTotalTime + animPeriod) % (long)(1000 * seqDuration);
      imPosition = (int) (animTotalTime / showPeriod);
      if ((imPosition == numImages-1) && (!isRepeating)) {  // end of sequence
        ticksIgnored = true;
        if (watcher != null)
          watcher.sequenceEnded(imName);
      }
    }
  }

  public BufferedImage getCurrentImage() {
    if (numImages != 0)
      return imsLoader.getImage(imName, imPosition);
    else
      return null;
  }

  public int getCurrentPosition() {
    return imPosition;
  }

  public void setWatcher(ImagesPlayerWatcher w) {
    watcher = w;
  }

  public void stop() {
    ticksIgnored = true;
  }

  public boolean isStopped() {
    return ticksIgnored;
  }

  public boolean atSequenceEnd() {
    return ((imPosition == numImages-1) && (!isRepeating));
  }

  public void restartAt(int imPosn) {
    if (numImages != 0) {
      if ((imPosn < 0) || (imPosn > numImages-1)) {
        System.err.println("Out of range restart, starting at 0");
        imPosn = 0;
      }

      imPosition = imPosn;
      animTotalTime = (long) imPosition * showPeriod;
      ticksIgnored = false;
    }
  }

  public void resume() {
    if (numImages != 0)
      ticksIgnored = false;
  } 
}

